/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */


#ifndef CORE_TRANSFORM_UNMARSHALLERCONTEXT_H_
#define CORE_TRANSFORM_UNMARSHALLERCONTEXT_H_

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include <map>
#include <string>

#include "HttpResponse.h"

namespace aliyun {

namespace transform {

// make sure input key exists in responseMap
class UnmarshallerContext {
 public:
  typedef std::string string;

  int integerValue(string key) {
    assert(responseMap_.find(key) != responseMap_.end());
    string value = responseMap_[key];
    char* tailptr = const_cast<char*>(value.c_str());
    int result = ::strtol(value.c_str(),
                          &tailptr, 10);

    assert(tailptr != value.c_str());
    return result;
  }

  string stringValue(string key) {
    assert(responseMap_.find(key) != responseMap_.end());
    return responseMap_[key];
  }

  bool boolValue(string key) {
    assert(responseMap_.find(key) != responseMap_.end());
    string value = responseMap_[key];
    return ::strncasecmp(value.c_str(), "true", value.length()) == 0;
  }

  float floatValue(string key) {
    assert(responseMap_.find(key) != responseMap_.end());
    string value = responseMap_[key];
    char* tailptr = const_cast<char*>(value.c_str());
    float result = ::strtof(value.c_str(), &tailptr);

    assert(tailptr != value.c_str());
    return result;
  }

  int lengthValue(string key) {
    return integerValue(key);
  }

  const http::HttpResponse& getHttpResponse() const {
    return httpResponse_;
  }

  void setHttpResponse(const http::HttpResponse& httpResponse) {
    httpResponse_ = httpResponse;
  }

  int getHttpStatus() const {
    return httpStatus_;
  }

  void setHttpStatus(int httpStatus) {
    httpStatus_ = httpStatus;
  }

  const std::map<string, string>& getResponseMap() const {
    return responseMap_;
  }

  void setResponseMap(const std::map<string, string>& responseMap) {
    responseMap_ = responseMap;
  }

 private:
  int httpStatus_;
  std::map<string, string> responseMap_;
  http::HttpResponse httpResponse_;
};

} // namespace transform

} // namespace aliyun

#endif /* CORE_TRANSFORM_UNMARSHALLERCONTEXT_H_ */
